package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.AnswerInfoDao;
import lj.courseinterect.dao.i.CourseInfoDao;
import lj.courseinterect.dao.i.QuestionInfoDao;
import lj.courseinterect.dao.i.StudentDao;
import lj.courseinterect.entity.AnswerInfo;
import lj.courseinterect.entity.CourseInfo;
import lj.courseinterect.entity.QuestionInfo;
import lj.courseinterect.entity.Student;
import lj.courseinterect.transfer.AnswerTransfer;
import lj.courseinterect.transfer.OperationTransfer;
import lj.courseinterect.transfer.QuestionBlockTranfer;
import lj.courseinterect.transfer.QuestionTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
@Path("/{a:tea|stu}/questions")
public class QuestionInfoResource
{
    final int PAGE_COUNT = 10;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private QuestionInfoDao questionInfoDao;

    @Autowired
    private StudentResource studentResource;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private CourseInfoDao courseInfoDao;

    @Autowired
    private AnswerInfoDao answerInfoDao;

    public Logger getLogger() {
        return logger;
    }

    public QuestionInfoDao getQuestionInfoDao() {
        return questionInfoDao;
    }

    public void setQuestionInfoDao(QuestionInfoDao questionInfoDao) {
        this.questionInfoDao = questionInfoDao;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer<QuestionInfo> create(QuestionInfo questionInfo)
    {
        this.logger.info("create(): " + questionInfo.getQueName());
        OperationTransfer<QuestionInfo> operationTransfer;
        Long stuId = studentResource.getStudent().getStuId();

        if(questionInfo.getStuId() != null && !stuId.equals(questionInfo.getStuId())){
            // 作者不对
            return new OperationTransfer<QuestionInfo>(false, "操作失败", null);
        }

        questionInfo.setStuId(stuId);
        if(questionInfo.getQueId() == null){
            // 添加操作
            questionInfo.setQueTime(new Date());
        }

        try {
            questionInfo = this.questionInfoDao.save(questionInfo);
            operationTransfer = new OperationTransfer<QuestionInfo>(true, "操作成功", questionInfo);
        } catch (Exception ex){
            operationTransfer = new OperationTransfer<QuestionInfo>(false, "操作失败", null);
        }

        this.logger.info("create(): " + operationTransfer.toString());
        return operationTransfer;
    }

    @GET
    @Path("/{queId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public QuestionTransfer getQuestionInfo(@PathParam("queId") Long queId){
        QuestionTransfer questionTransfer = new QuestionTransfer();
        QuestionInfo questionInfo = questionInfoDao.find(queId);
        questionTransfer.setQuestionInfo(questionInfo);
        if(questionInfo != null){
            Student student = studentDao.find(questionInfo.getStuId());
            questionTransfer.setStudent(student);

            CourseInfo courseInfo = courseInfoDao.find(questionInfo.getCouId());
            questionTransfer.setCourseInfo(courseInfo);
        }
        return questionTransfer;
    }

    @POST
    @Path("/{queId: [0-9]*$}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public OperationTransfer updateQuestionInfo(@PathParam("queId") Long queId, QuestionInfo questionInfo){
        OperationTransfer operationTransfer;
        QuestionInfo questionInfo1 = questionInfoDao.find(queId);
        Long stuId = studentResource.getStudent().getStuId();
        if(questionInfo1 != null && questionInfo1.getStuId().equals(stuId)){
            questionInfoDao.save(questionInfo);
            operationTransfer = new OperationTransfer(true, "更新成功");
        } else {
            operationTransfer = new OperationTransfer(false, "没有权限修改该问题");
        }

        return operationTransfer;
    }

    /**
     * 按时间顺序分页获取课程学识区域问题块
     * @param couId
     * @param pageNum
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getByTime/{couId}/{pageNum}")
    public List<QuestionBlockTranfer> getQuestionBlockByCouIdOrderByTime(@PathParam("couId") Long couId, @PathParam("pageNum") int pageNum){
        List<QuestionInfo> questionInfos = questionInfoDao.findByCouIdOrderByTime(couId, pageNum, PAGE_COUNT);
        List<QuestionBlockTranfer> questionBlockTranfers = new ArrayList<QuestionBlockTranfer>();

        CourseInfo courseInfo = courseInfoDao.find(couId);

        for(QuestionInfo questionInfo : questionInfos){
            QuestionBlockTranfer questionBlockTranfer = new QuestionBlockTranfer(1);
            Student student = studentDao.find(questionInfo.getStuId());
            List<AnswerInfo> answerInfos = answerInfoDao.findByQueId(questionInfo.getQueId());

            questionBlockTranfer.setCourseInfo(courseInfo);
            questionBlockTranfer.setStudent(student);
            questionBlockTranfer.setAnswerInfos(answerInfos);
            questionBlockTranfer.setQuestionInfo(questionInfo);

            questionBlockTranfers.add(questionBlockTranfer);
        }

        return questionBlockTranfers;
    }

    /**
     * 按赞顺序分页获取课程学识区域问题块
     * @param couId
     * @param pageNum
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getByGood/{couId}/{pageNum}")
    public List<QuestionBlockTranfer> getQuestionBlockByCouIdOrderByGood(@PathParam("couId") Long couId, @PathParam("pageNum") int pageNum){
        List<QuestionInfo> questionInfos = questionInfoDao.findByCouIdOrderByGood(couId, pageNum, PAGE_COUNT);
        List<QuestionBlockTranfer> questionBlockTranfers = new ArrayList<QuestionBlockTranfer>();

        CourseInfo courseInfo = courseInfoDao.find(couId);

        for(QuestionInfo questionInfo : questionInfos){
            QuestionBlockTranfer questionBlockTranfer = new QuestionBlockTranfer(1);
            Student student = studentDao.find(questionInfo.getStuId());
            List<AnswerInfo> answerInfos = answerInfoDao.queryByQueIdOrderByGood(questionInfo.getQueId());

            questionBlockTranfer.setCourseInfo(courseInfo);
            questionBlockTranfer.setStudent(student);
            questionBlockTranfer.setAnswerInfos(answerInfos);
            questionBlockTranfer.setQuestionInfo(questionInfo);

            if(!questionBlockTranfer.getAnswerInfos().isEmpty()){
                // 热门不加答案为空的
                questionBlockTranfers.add(questionBlockTranfer);
            }
        }

        return questionBlockTranfers;
    }

    /**
     * 按时间顺序分页获取天大问题块
     * @param pageNum
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getAllByTime/{couId}/{pageNum}")
    public List<QuestionBlockTranfer> getQuestionBlockOrderByTime(@PathParam("pageNum") int pageNum){
        List<QuestionInfo> questionInfos = questionInfoDao.findAllOrderByTime(pageNum, PAGE_COUNT);
        List<QuestionBlockTranfer> questionBlockTranfers = new ArrayList<QuestionBlockTranfer>();

//        CourseInfo courseInfo = courseInfoDao.find(Long.getLong(String.valueOf(0)));

        for(QuestionInfo questionInfo : questionInfos){
            QuestionBlockTranfer questionBlockTranfer = new QuestionBlockTranfer(1);
            Student student = studentDao.find(questionInfo.getStuId());
            List<AnswerInfo> answerInfos = answerInfoDao.findByQueId(questionInfo.getQueId());

            CourseInfo courseInfo = courseInfoDao.find(questionInfo.getCouId());
            questionBlockTranfer.setCourseInfo(courseInfo);
            questionBlockTranfer.setStudent(student);
            questionBlockTranfer.setAnswerInfos(answerInfos);
            questionBlockTranfer.setQuestionInfo(questionInfo);

            questionBlockTranfers.add(questionBlockTranfer);
        }

        return questionBlockTranfers;
    }

    /**
     * 按赞顺序分页获取天大问题块
     * @param pageNum
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getAllByGood/{couId}/{pageNum}")
    public List<QuestionBlockTranfer> getQuestionBlockOrderByGood(@PathParam("pageNum") int pageNum){
        List<QuestionInfo> questionInfos = questionInfoDao.findAllOrderByGood(pageNum, PAGE_COUNT);
        List<QuestionBlockTranfer> questionBlockTranfers = new ArrayList<QuestionBlockTranfer>();

//        CourseInfo courseInfo = courseInfoDao.find(Long.getLong(String.valueOf(0)));

        for(QuestionInfo questionInfo : questionInfos){
            QuestionBlockTranfer questionBlockTranfer = new QuestionBlockTranfer(1);
            Student student = studentDao.find(questionInfo.getStuId());
            List<AnswerInfo> answerInfos = answerInfoDao.queryByQueIdOrderByGood(questionInfo.getQueId());

            CourseInfo courseInfo = courseInfoDao.find(questionInfo.getCouId());
            questionBlockTranfer.setCourseInfo(courseInfo);
            questionBlockTranfer.setStudent(student);
            questionBlockTranfer.setAnswerInfos(answerInfos);
            questionBlockTranfer.setQuestionInfo(questionInfo);

            if(!questionBlockTranfer.getAnswerInfos().isEmpty()){
                // 热门不加答案为空的
                questionBlockTranfers.add(questionBlockTranfer);
            }
        }

        return questionBlockTranfers;
    }


    /**
     * 按赞顺序分页获取天大问题块
     * @param pageNum
     * @return
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/getQuestionBySelectedCourse/{couId}/{pageNum}")
    public List<QuestionBlockTranfer> getQuestionBySelectedCourse(@PathParam("pageNum") int pageNum){
        List ids = questionInfoDao.querySelectedCourseByStuId(studentResource.getStudent().getStuId(), pageNum, PAGE_COUNT);
        List<QuestionBlockTranfer> questionBlockTranfers = new ArrayList<QuestionBlockTranfer>();

//        CourseInfo courseInfo = courseInfoDao.find(Long.getLong(String.valueOf(0)));

        for(int i = 0; i < ids.size(); i++){
            // q.queId, s.stuId, sc.scId
            QuestionBlockTranfer questionBlockTranfer = new QuestionBlockTranfer(1);
            Object[] object = (Object[]) ids.get(i);
            QuestionInfo questionInfo = questionInfoDao.find((Long)object[0]);
            Student student = studentDao.find(questionInfo.getStuId());
            CourseInfo courseInfo = courseInfoDao.find(questionInfo.getCouId());
            List<AnswerInfo> answerInfos = answerInfoDao.queryByQueIdOrderByGood(questionInfo.getQueId());

            questionBlockTranfer.setStudent(student);
            questionBlockTranfer.setQuestionInfo(questionInfo);
            questionBlockTranfer.setAnswerInfos(answerInfos);
            questionBlockTranfer.setCourseInfo(courseInfo);

            if(!questionBlockTranfer.getAnswerInfos().isEmpty()){
                questionBlockTranfers.add(questionBlockTranfer);
            }
        }

        return questionBlockTranfers;
    }
}